﻿using FastExecutor.Base.Util;
using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastUtil.Base;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using FastIOC.Annotation;

namespace FastExecutor.ORG.Controller
{

    [RouteArea("Frame")]
    [RoutePrefix("User")]
    public class UserController : FastTemplateController<Frame_User>
    {
        [AutoWired]
        public DB_OU @DB_OU;
        [AutoWired]
        public DB_Role @DB_Role;
        [AutoWired]
        public DB_UserRoleRelation @DB_UserRoleRelation;
        [AutoWired]
        public DB_User @DB_User;
        [AutoWired]
        public DB_Attach @DB_Attach;

        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/User/User_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/User/User_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/User/User_Edit.cshtml");
        }

        [Route("Modify")]
        public ActionResult Modify()
        {
            return View("~/Views/Frame/User/UserModify_Edit.cshtml");
        }

        [Route("ModifyPWD")]
        public ActionResult ModifyPWD()
        {
            return View("~/Views/Frame/User/User_ModifyPWD.cshtml");
        }

        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {

            JObject Params = GetParams();
            string DisplayName = Convert.ToString(Params["DisplayName"]);
            string LoginID = Convert.ToString(Params["LoginID"]);
            string OUGuid = Convert.ToString(Params["OUGuid"]);
            bool SearchInAll = Convert.ToBoolean(Params["SearchInAll"]);
            if (SearchInAll)
            {
                if (!string.IsNullOrEmpty(DisplayName))
                {
                    ListWhereText += string.Format(" and DisplayName like '%{0}%'", DisplayName);
                }
                if (!string.IsNullOrEmpty(LoginID))
                {
                    ListWhereText += string.Format(" and LoginID like '%{0}%'", LoginID);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(DisplayName))
                {
                    ListWhereText += string.Format(" and DisplayName like '%{0}%'", DisplayName);
                }
                if (!string.IsNullOrEmpty(LoginID))
                {
                    ListWhereText += string.Format(" and LoginID like '%{0}%'", LoginID);
                }
                if (!string.IsNullOrEmpty(OUGuid))
                {
                    if (OUGuid != "Top")
                    {
                        ListWhereText += string.Format(" and OUGuid='{0}'", OUGuid);
                    }

                }
            }

            ReturnValue result = (ReturnValue)base.FindList();
            JArray array = JArray.FromObject((List<Frame_User>)result.Get("data"));
            foreach (JObject item in array)
            {
                string RowGuid = Convert.ToString(item.GetValue("OUGuid"));
                item.Add("OUName",@DB_OU.GetFullPathOUName(RowGuid));
            }
            result.Add("data", array);
            return result;
        }


        [HttpPost, Route("FindRelationList")]
        public ActionResult FindRelationList()
        {

            JObject Params = GetParams();
            string DisplayName = Convert.ToString(Params["DisplayName"]);
            string LoginID = Convert.ToString(Params["LoginID"]);
            string OUGuid = Convert.ToString(Params["OUGuid"]);
            bool SearchInAll = Convert.ToBoolean(Params["SearchInAll"]);
            if (SearchInAll)
            {
                if (!string.IsNullOrEmpty(DisplayName))
                {
                    ListWhereText += string.Format(" and DisplayName like '%{0}%'", DisplayName);
                }
                if (!string.IsNullOrEmpty(LoginID))
                {
                    ListWhereText += string.Format(" and LoginID like '%{0}%'", LoginID);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(DisplayName))
                {
                    ListWhereText += string.Format(" and DisplayName like '%{0}%'", DisplayName);
                }
                if (!string.IsNullOrEmpty(LoginID))
                {
                    ListWhereText += string.Format(" and LoginID like '%{0}%'", LoginID);
                }
                if (!string.IsNullOrEmpty(OUGuid))
                {
                    if (OUGuid != "Top")
                    {
                        ListWhereText += string.Format(" and OUGuid='{0}'", OUGuid);
                    }

                }
            }

            ReturnValue result = (ReturnValue)base.FindList();
            JArray array = JArray.FromObject((List<Frame_User>)result.Get("data"));
            List<Frame_Role> RoleList = @DB_Role.FindAll();
            List<Frame_UserRolerelation> UserRelationList = @DB_UserRoleRelation.FindAll();
            foreach (JObject item in array)
            {
                string UserGuid = Convert.ToString(item.GetValue("RowGuid"));
                List<Frame_Role> CopyRoleList = CopyUtil.CopyList(RoleList);
                List<Frame_UserRolerelation> RelationFilter = null;
                foreach (Frame_Role Role in CopyRoleList)
                {
                    RelationFilter = UserRelationList.Where(a => a.UserGuid == UserGuid && a.RoleGuid == Role.RowGuid).ToList();
                    if (RelationFilter.Count > 0)
                    {
                        Role.IsCheck = true;
                    }
                    else
                    {
                        Role.IsCheck = false;
                    }
                }
                item.Add("RoleList", JsonConvert.SerializeObject(CopyRoleList));

            }
            result.Add("data", array);
            return result;
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("FindRelationDetail")]
        public ActionResult FindRelationDetail()
        {
            JObject Params = GetParams();
            ReturnValue result = (ReturnValue)base.FindDetail();
            JObject obj = JObject.FromObject((Frame_User)result.Get("data"));
            string UserGuid = Convert.ToString(obj.GetValue("RowGuid"));
            List<Frame_Role> RoleList = @DB_Role.FindAll();
            List<Frame_UserRolerelation> UserRelationList = @DB_UserRoleRelation.FindAll();
            List<Frame_UserRolerelation> RelationFilter = null;
            foreach (Frame_Role Role in RoleList)
            {
                RelationFilter = UserRelationList.Where(a => a.UserGuid == UserGuid && a.RoleGuid == Role.RowGuid).ToList();
                if (RelationFilter.Count > 0)
                {
                    Role.IsCheck = true;
                }
                else
                {
                    Role.IsCheck = false;
                }
            }
            obj.Add("RoleList", JsonConvert.SerializeObject(RoleList));
            result.Add("data", obj);
            return result;
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_User Entity)
        {
            return base.OperateAdd(Entity);
        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }

        [HttpPost, Route("InitPWD")]
        public ActionResult InitPWD()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string RowGuid = Convert.ToString(Params["RowGuid"]);
            @DB_User.InitPWD(RowGuid);
            return result;
        }

        [HttpPost, Route("ChangeEnable")]
        public ActionResult ChangeEnable()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string RowGuid = Convert.ToString(Params["RowGuid"]);
            int Enable = Convert.ToInt32(Params["Enable"]);
            @DB_User.ChangeEnable(RowGuid, Enable);
            return result;
        }

        [HttpPost, Route("GetHomeInfo")]
        public ActionResult GetHomeInfo()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            result.Add("DisplayName", DisplayName);
            result.Add("UserGuid", UserGuid);
            string AttachGuid = string.Empty;
            bool ExistHeadImg = @DB_Attach.ExistAttach(UserGuid, "UserHeadIMG", out AttachGuid);
            result.Add("ExistHeadImg", ExistHeadImg);
            result.Add("AttachGuid", AttachGuid);
            return result;
        }

        [HttpPost, Route("OperateModifyPWD")]
        public ActionResult OperateModifyPWD()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string PassWord = Convert.ToString(Params.GetValue("PassWord"));
            string NewPassWord = Convert.ToString(Params.GetValue("NewPassWord"));
            Frame_User Entity = @DB_User.FindOne(UserGuid);
            if (Entity.PassWord == EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + PassWord))
            {
                Entity.PassWord = EncryptUtil.MD5Encrypt(Entity.LoginID + "|" + NewPassWord);
                @DB_User.Update(Entity);
            }
            else
            {
                result.Add("code", -100);
                result.Add("msg", "原密码错误");
            }
            return result;
        }

        [Route("Tree")]
        public ActionResult Tree()
        {
            ReturnValue result = new ReturnValue();
            List<Frame_OU> AllOUList = @DB_OU.FindAll("", "OrderNum desc");
            List<Tree> AllTreeList = new List<Tree>();
            foreach (Frame_OU item in AllOUList)
            {
                Tree treenode = new Tree();
                treenode.id = item.RowGuid;
                treenode.label = item.OUName;
                treenode.paerntGuid = item.ParentOUGuid;
                treenode.AddAtttibute("oucode", item.OUCode);
                AllTreeList.Add(treenode);
            }

            List<Frame_User> AllUserList = DB_Fast.FindAll("", "OrderNum desc");
            foreach (Frame_OU item in AllOUList)
            {
                List<Frame_User> UserFilterList = AllUserList.Where(a => a.OUGuid == item.RowGuid).ToList();
                foreach (Frame_User User in UserFilterList)
                {
                    Tree treenode = new Tree();
                    treenode.id = User.RowGuid;
                    treenode.label = User.DisplayName;
                    treenode.paerntGuid = item.RowGuid;
                    AllTreeList.Add(treenode);
                }
            }


            List<Tree> TreeNodeList = TreeBuilder.Build("Top", AllTreeList);
            result.Add("tree", TreeNodeList);
            return result;
        }
    }
}
